﻿using Kendo.Mvc.UI;
using Scorpicore.Data;
using Scorpicore.Models;
using Scorpicore.MVC.Models;
using System;
using Kendo.Mvc.Extensions;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Scorpicore.MVC.ViewModels;

namespace Scorpicore.MVC.Controllers
{

    [ValidateInput(false)]
    public class HomeController : Controller
    {
        IUnitOfWork db;

        public HomeController(IUnitOfWork db)
        {
            this.db = db;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Ideas()
        {
            return View();
        }

        public ActionResult SearchByUsers()
        {
            var users = db.Users.All().Select(u => new TopTenUsersViewModel
            {
                Id = u.Id,
                AvatarUrl = u.AvatarUrl,
                UserName = u.UserName,
                Banknotes = u.Banknotes.Count,
                Tracks = u.Tracks.Count
            }).AsEnumerable();
            return View(users);
        }


        public JsonResult ReadUsersTracks([DataSourceRequest]DataSourceRequest request, string id)
        {
            var user = db.Users.All().FirstOrDefault(u => u.Id == id);
            var result = user.Tracks
                .Select(x => new TracksInTopTenBanknotesViewModel()
                            {
                                Date = x.Date,
                                Id = x.Id,
                                Message = x.Message,
                                Place = x.Place.Name,
                                User = x.User.UserName
                            }).ToList();

            return Json(result.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }


        public ActionResult ResultSearchUsersByStringText(string name)
        {
            return View("ResultSearchUsersByStringText", "Home", name);
        }

        public JsonResult ReadUsers([DataSourceRequest]DataSourceRequest request)
        {

            var result = db.Users.All().Select(u => new TopTenUsersViewModel
            {
                Id = u.Id,
                AvatarUrl = u.AvatarUrl,
                UserName = u.UserName,
                Banknotes = u.Banknotes.Count,
                Tracks = u.Tracks.Count
            }).AsEnumerable().OrderByDescending(b => b.Banknotes);

            return Json(result.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        [Authorize]
        public ActionResult PrivateBanknotes()
        {
            return View();
        }

        [Authorize]
        public JsonResult ReadBanknotes([DataSourceRequest] DataSourceRequest request)
        {
            var name = User.Identity.Name;
            var user = db.Users.All().FirstOrDefault(u => u.UserName == name);
            var model = db.Tracks.All().Where(t => t.User.UserName == name).Select(x => new PrivateBanknotesViewModel
                {
                    Id = x.Banknote.Id,
                    Denomination = x.Banknote.Denomination,
                    Distance = x.Banknote.Distance,
                    ImageUrl = x.Banknote.ImageUrl,
                    LastPlace = x.Banknote.LastPlace,
                    Letters = x.Banknote.Letters,
                    SerialNumber = x.Banknote.SerialNumber
                });
            var result = Json(model.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

            return result;
        }

        public ActionResult GetNameToSearch(string name)
        {
            var user = db.Users.All().FirstOrDefault(u => u.UserName == name);
            if (user == null)
            {
                return View("UserError");
            }

            var model = new TopTenUsersViewModel()
            {
                Id = user.Id,
                TracksList = user.Tracks.Select(x => new TracksInTopTenBanknotesViewModel()
                                                        {
                                                            Date = x.Date,
                                                            Id = x.Id,
                                                            Message = x.Message,
                                                            Place = x.Place.Name,
                                                            User = x.User.UserName
                                                        }).ToList(),
                BanknotesList = user.Banknotes.Select(x => new TopTenBanknoteViewModel()
                                                        {
                                                            Denomination = x.Denomination,
                                                            Distance = x.Distance,
                                                            Id = x.Id,
                                                            ImageUrl = x.ImageUrl,
                                                            LastPlace = x.LastPlace,
                                                            Letters = x.Letters,
                                                            SerialNumber = x.SerialNumber
                                                        }).ToList(),
                AvatarUrl = user.AvatarUrl,
                Banknotes = user.Banknotes.Count(),
                Tracks = user.Tracks.Count,
                UserName = user.UserName
            };

            return View(model);
        }

        public ActionResult UserError(ErrorViewModel model)
        {
            return View(model);
        }

    }
}